<template>
  <div class="app-container">
    <el-form :model="queryParams" label-width="120px" inline>
      <el-form-item label="个人缴费比例">
        <el-input
          v-model="queryParams.personalPaymentRate"
          placeholder="请输入个人缴费比例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="单位实缴金额">
        <el-input
          v-model="queryParams.employerActualPayment"
          placeholder="请输入单位实缴金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="个人实缴金额">
        <el-input
          v-model="queryParams.personalActualPayment"
          placeholder="请输入个人实缴金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="缴费标识">
        <el-select
          v-model="queryParams.paymentStatus"
          placeholder="请选择缴费标识"
          clearable
        >
          <el-option label="已缴费" value="已缴费" />
          <el-option label="未缴费" value="未缴费" />
          <el-option label="部分缴费" value="部分缴费" />
        </el-select>
      </el-form-item>

      <!-- 搜索按钮 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:medicalInsurance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:medicalInsurance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:medicalInsurance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:medicalInsurance:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="upload.open = true"
          v-hasPermi="['project:medicalInsurance:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="medicalInsuranceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="缴费明细ID" align="center" prop="id" />
      <el-table-column label="个人缴费比例" align="center" prop="personalPaymentRate" />
      <el-table-column label="单位实缴金额" align="center" prop="employerActualPayment" />
      <el-table-column label="单位实缴划入个人账户金额" align="center" prop="employerToPersonalAccount" />
      <el-table-column label="个人实缴金额" align="center" prop="personalActualPayment" />
      <el-table-column label="个人缴费划入个人账户金额" align="center" prop="personalToPersonalAccount" />
      <el-table-column label="缴费标识" align="center" prop="paymentStatus" />
      <el-table-column label="行政职务" align="center" prop="administrativePosition" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:medicalInsurance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:medicalInsurance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医疗保险对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="个人缴费比例" prop="personalPaymentRate">
          <el-input v-model="form.personalPaymentRate" placeholder="请输入个人缴费比例" />
        </el-form-item>
        <el-form-item label="单位实缴金额" prop="employerActualPayment">
          <el-input v-model="form.employerActualPayment" placeholder="请输入单位实缴金额" />
        </el-form-item>
        <el-form-item label="单位实缴划入个人账户金额" prop="employerToPersonalAccount">
          <el-input v-model="form.employerToPersonalAccount" placeholder="请输入单位实缴划入个人账户金额" />
        </el-form-item>
        <el-form-item label="个人实缴金额" prop="personalActualPayment">
          <el-input v-model="form.personalActualPayment" placeholder="请输入个人实缴金额" />
        </el-form-item>
        <el-form-item label="个人缴费划入个人账户金额" prop="personalToPersonalAccount">
          <el-input v-model="form.personalToPersonalAccount" placeholder="请输入个人缴费划入个人账户金额" />
        </el-form-item>
        <el-form-item label="缴费标识" prop="paymentStatus">
          <el-select v-model="form.paymentStatus" placeholder="请选择缴费标识" clearable>
            <el-option label="已缴费" value="已缴费" />
            <el-option label="未缴费" value="未缴费" />
            <el-option label="部分缴费" value="部分缴费" />
          </el-select>
        </el-form-item>
        <el-form-item label="行政职务" prop="administrativePosition">
          <el-input v-model="form.administrativePosition" placeholder="请输入行政职务" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="导入医疗保险数据"
      :visible.sync="upload.open"
      width="500px"
      :before-close="() => upload.open = false"
    >
      <el-upload
        ref="upload"
        drag
        :limit="1"
        accept=".xlsx,.xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        style="width: 100%;"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip" class="el-upload__tip">
          <el-checkbox v-model="upload.updateSupport">是否更新已存在数据</el-checkbox>
          <br />仅支持 .xls、.xlsx 文件
          <el-link type="primary" @click="importTemplate" style="margin-left: 10px;">下载模板</el-link>
        </div>
      </el-upload>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMedicalInsurance, getMedicalInsurance, delMedicalInsurance, addMedicalInsurance, updateMedicalInsurance, importData } from "@/api/project/medicalInsurance"

export default {
  name: "MedicalInsurance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医疗保险表格数据
      medicalInsuranceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personalPaymentRate: null,
        employerActualPayment: null,
        employerToPersonalAccount: null,
        personalActualPayment: null,
        personalToPersonalAccount: null,
        paymentStatus: null,
        administrativePosition: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personalPaymentRate: [
          { validator: (rule, value, callback) => {
              if (value && (value < 0 || value > 100)) {
                callback(new Error('个人缴费比例必须在0到100之间'));
              } else {
                callback();
              }
            }, trigger: 'blur' }
        ],
        employerActualPayment: [
          { validator: (rule, value, callback) => {
              if (value && value < 0) {
                callback(new Error('单位实缴金额不能为负数'));
              } else {
                callback();
              }
            }, trigger: 'blur' }
        ],
        personalActualPayment: [
          { validator: (rule, value, callback) => {
              if (value && value < 0) {
                callback(new Error('个人实缴金额不能为负数'));
              } else {
                callback();
              }
            }, trigger: 'blur' }
        ],
        paymentStatus: [
          { validator: (rule, value, callback) => {
              if (value && !['已缴费', '未缴费', '部分缴费'].includes(value)) {
                callback(new Error('缴费标识无效'));
              } else {
                callback();
              }
            }, trigger: 'blur' }
        ]
      },
      upload: {
        open: false,
        url: process.env.VUE_APP_BASE_API + "/project/medicalInsurance/importData",
        headers: {}, // 注意不要在 data() 中使用 this
        updateSupport: false,
        isUploading: false
      }
    }
  },
  created() {
    this.getList();
    this.upload.headers = {
      Authorization: "Bearer " + this.$store.getters.token
    };
  },
  methods: {
    /** 查询医疗保险列表 */
    getList() {
      this.loading = true
      listMedicalInsurance(this.queryParams).then(response => {
        this.medicalInsuranceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        personalPaymentRate: null,
        employerActualPayment: null,
        employerToPersonalAccount: null,
        personalActualPayment: null,
        personalToPersonalAccount: null,
        paymentStatus: null,
        administrativePosition: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        personalPaymentRate: null,
        employerActualPayment: null,
        personalActualPayment: null,
        paymentStatus: null,
        pageNum: 1,
        pageSize: 10
      };
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加医疗保险"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMedicalInsurance(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改医疗保险"
      })
    },
    /** 提交表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            // 修改数据
            updateMedicalInsurance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              this.$modal.msgError(error.response.data.msg || "修改失败");
            });
          } else {
            // 新增数据
            addMedicalInsurance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(error => {
              this.$modal.msgError(error.response.data.msg || "新增失败");
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除医疗保险编号为"' + ids + '"的数据项？').then(function() {
        return delMedicalInsurance(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/medicalInsurance/export', {
        ...this.queryParams
      }, `medicalInsurance_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    submitFileForm() {
      this.$refs.upload.submit();
    },
    importTemplate() {
      this.download('/project/medicalInsurance/importTemplate', {}, '医疗保险导入模板.xlsx');
    }
  }
}
</script>
